package com.neusoft.mybatisplustest.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.mybatisplustest.mapper.DeptPlusMapper;
import com.neusoft.mybatisplustest.po.Dept;
import com.neusoft.mybatisplustest.service.DeptPlusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptPlusServiceImpl implements DeptPlusService {

    @Autowired
    DeptPlusMapper deptPlusMapper;
    @Override
    public List<Dept> getDeptPulss() {
        return deptPlusMapper.selectList(null);
    }

    @Override
    public Dept getDeptById(Integer deptno) {
        return deptPlusMapper.selectById(deptno);
    }

    @Override
    public int insertDept(Dept dept) {
        return deptPlusMapper.insert(dept);
    }

    @Override
    public int updateDept(Dept dept) {
        return deptPlusMapper.updateById(dept);
    }

    @Override
    public int deleteDept(Integer deptno) {
        return deptPlusMapper.deleteById(deptno);
    }

    @Override
    public List<Dept> getDeptByCondition(Integer deptno, String loc) {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("deptno", 40); //deptno大于40
        queryWrapper.like("loc","沈阳");

        return deptPlusMapper.selectList(queryWrapper) ;
    }

    @Override
    public int updateDeptByCondition(Dept dept) {
        UpdateWrapper<Dept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("dname",dept.getDname());
        updateWrapper.set("loc",dept.getLoc());
        updateWrapper.eq("deptno",dept.getDeptno());
        return deptPlusMapper.update(null,updateWrapper) ;
    }

    @Override
    public Map<String,Object> getDeptByPage(Integer pageNum, Integer pageSize) {
        //创建IPage对象，设置参数：第几页、每页显示最大行数
        IPage<Dept> page = new Page<>(pageNum, pageSize);
        //调用selectPage方法进行分页查询，返回值为IPage对象。
        IPage<Dept> deptPage = deptPlusMapper.selectPage(page, null);
        //获取当前页数据
        List<Dept> list = deptPage.getRecords();
        //获取总记录数
//        List<Dept> totalList =  deptPlusMapper.selectList(null);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);//当前页数据
        map.put("total",deptPage.getTotal());//总记录数
        return map;
    }

    @Override
    public List<Dept> selectDeptAllByMapper() {
        return deptPlusMapper.selectDeptAllByMapper();
    }
}
